package com.zhuoshi.utils;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import com.zhuoshi.bean.AreaBean;


public class ExcelHelper {
	
	@SuppressWarnings("resource")
	public void getExcel(String sheetName,OutputStream output) throws Exception {
		//第一步,创建一个webbook文件,对应一个excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二部,在excel中添加一个sheet工作簿,参数为该工作簿名字,不写为默认;
        HSSFSheet sheet = wb.createSheet("地域表");
        //第三部,做sheet中添加表头第0行,注意老版本poi对excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);
        //第四部,创建单元格表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        //style.setAlignment(HSSFCellStyle);//创建一个居中格式
        
        //创建具体盛放数据的单元格,可以考虑把cell抽成共通对象去使用
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("ID");
        cell = row.createCell(1);
        cell.setCellValue("地域");
//        cell = row.createCell((int) 2);
//        cell.setCellValue("年龄");
//        cell = row.createCell((int) 3);
//        cell.setCellValue("生日");
        
        //第五部,写入实体数据 实际应用中这些数据应该是从数据库中得到
        List<AreaBean> list = getArea();
        for(int i=0;i<list.size();i++){
            //每次新建一行然后在新行中插入list中的数据对象,有点繁琐,也许有更好的封装方法,留待后看
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(list.get(i).getId());
            row.createCell(1).setCellValue(list.get(i).getAreaname());
//            row.createCell((int)2).setCellValue(list.get(i).getAge());
//            row.createCell((int)3).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getBirth()));
            
        }
        
        //第六部,将文件保存到指定位置
        //FileOutputStream fout = new FileOutputStream("D:/area.xls");
        wb.write(output);
        output.close();
		
	}
	
	public static List<AreaBean> getArea(){
		List<AreaBean> list = new ArrayList<AreaBean>();
		AreaBean a1 = new AreaBean(1,"东北");
		AreaBean a2 = new AreaBean(2,"西北");
		AreaBean a3 = new AreaBean(3,"南北");
		AreaBean a4 = new AreaBean(4,"华北");
		AreaBean a5 = new AreaBean(5,"东南");
		list.add(a1);
		list.add(a2);
		list.add(a3);
		list.add(a4);
		list.add(a5);
		return list;
	}
	

}
